package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementServiceImpl statementService;
    @Autowired
    private AppointmentServiceImpl appointmentService;

    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        // 判断
        if (qo.getStatementId()==null) {
            new BusinessException("不可强行访问此页面");
        }
        List<StatementItem> statementItems = statementItemMapper.selectForList(qo);
        if (statementItems == null) {
            new BusinessException("该结算单不存在");
        }
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        return new TablePageInfo<>(statementItems);
    }

    @Override
    public void saveItems(List<StatementItem> items) {
        //获取最后一条记录
        StatementItem tempItem = items.remove(items.size() - 1);
        //获取前台传入的折扣金额
        BigDecimal discountAmount = tempItem.getItemPrice();
        //合理性校验--->判断结算单的状态,是否是消费中
        Long statementId = tempItem.getStatementId();
        //根据id获取结算单对象
        Statement statement = statementService.get(statementId);
        //如果是已经支付的状态,不能保存明细
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("不能对已支付的结算单进行保持明细操作");
        }
        //删除之前的明细项集合
        statementItemMapper.deleteByStatementId(statement.getId());
        //计算整个明细的总金额和总数量
        BigDecimal totalAmount = new BigDecimal(0);
        BigDecimal totalQuantity = new BigDecimal(0);
        if (items.size()>0) {
            for (StatementItem item : items) {
                //循环的过程中,把每个结算单明细保存到数据库中
                statementItemMapper.insert(item);
                //循环的过程中,明细的单价 * 数量--->累加到总金额中
                totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
                totalQuantity = totalQuantity.add(item.getItemQuantity());
            }
        }
        //如果优惠金额大于总金额,属于非法操作
        if (totalAmount.compareTo(discountAmount) < 0) {
            throw new BusinessException("非法操作,优惠金额大于总金额");
        }
        //更新结算单信息
        statementService.updateAmount(statement.getId(),totalAmount,totalQuantity,discountAmount);
    }

    @Override
    public void payStatement(Long statementId) {
        // 合理性校验:已经支付了,就不要重复支付了
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("结算单已经支付,请不要重复支付");
        }
        //更新结算单的状态 (ShiroUtils.getUserId() 获取登录的用户)
        statementService.updatePayStatus(statementId,Statement.STATUS_PAID, ShiroUtils.getUserId());
        if (statement.getAppointmentId() != null) {
            //说明结算单是预约单生成的
            //修改预约单的状态
            appointmentService.updateStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public void removeByStatementId(Long statementId) {
        statementItemMapper.deleteByStatementId(statementId);
    }
}
